package com.example.fashionshoes.utils;

import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import com.example.fashionshoes.constant.JwtConst;

import java.util.Map;

public class JwtUtil {
    private static final String KEY = "Tomorrow";

    //接收业务数据,生成token并返回
    public static String getToken(Map<String, Object> claims) {
        return JWT.create()
                .withClaim(JwtConst.CLAIMS, claims)
//                .withExpiresAt(new Date(System.currentTimeMillis() + 1000 * 60 * 60 * 12))
                .sign(Algorithm.HMAC256(KEY));
    }

    //接收token,验证token,并返回业务数据
    public static Map<String, Object> parseToken(String token) {
        String prefix = JwtConst.PREFIX;
        String subToken = token.substring(prefix.length());
        return JWT.require(Algorithm.HMAC256(KEY))
                .build()
                .verify(subToken)
                .getClaim(JwtConst.CLAIMS)
                .asMap();
    }
}
